<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel='stylesheet' href='../rurple.css' type='text/css'>
<title>Octal, hexadecimal and binary numbers</title>
</head>
<body>
<h2 class="title">Octal, hexadecimal and binary numbers</h3>
<p><span class="warning">Note:</span> you do not need to completely understand the content of this section to continue learning about Python.</p>
<p>In lesson 36 lesson, I told you that some error messages or other unexpected results could occur if you tried with different numbers than the one I tried.  Here are two such examples:</p>
<pre>
&gt;&gt;&gt; 1,023
(1, 19)

&gt;&gt;&gt; 1,099
  File <span class="string">"&lt;input&gt;"</span>, line 1
    1, 099
         ^
SyntaxError: invalid token
</pre>
<p>What's happening here is that Python interprets any number that start with a zero as being an octal number, that is a number expressed in base 8.</p>
<p>"Usual" numbers are expressed in base 10.  They are made up of the 10 usual symbols: "0, 1, 2, 3, 4, 5, 6, 7, 8 and 9".  A number like 345 is interpreted to mean:</p>
<pre>
345 = 3*100 + 4*10 + 5*1
or
345 = 3*10*10 + 4*10 + 5*1
or
345 = 3*10**2 + 4*10**1 + 5*10**0
</pre>
<p><small>[If the last one does not make any sense to you, it means that you still have to learn more mathematics in school!  We have seen earlier what "**" means.]</small>  By comparison, a number in base 8 is made up of the following 8 symbols: "0, 1, 2, 3, 4, 5, 6 and 7".  A number like "23" (or 023 in Python; that is "zero"23) in base 8 is meant to mean:<p>
<pre>
023 = 2*8 + 3*1 = 16 + 3 = 19 (in base 10)
</pre>
<p>This should explain the first result we showed above.  The second comes from the fact that "9" is not an allowed symbol in base 8.</p>
<p>Confused?...  Let's add to your confusion, in the hope of clarifying things!  Python also knows about hexadecimal numbers (numbers in base 16).  They are usually written as a combinations of the following 16 symbols: "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f".  We indicate to Python that we are using an hexadecimal number by starting with the two characters "0x" (zero followed by x) like the following:</p>
<pre>
&gt;&gt;&gt; 0x33
51

&gt;&gt;&gt; 0xa
10

&gt;&gt;&gt; 0xf
15

&gt;&gt;&gt; 0x10
16

&gt;&gt;&gt; 0x111
273

&gt;&gt;&gt; 0x123
291
</pre>
<p>Let's see if we understand, starting with the last one:</p>
<pre>
0x123 = 1*16*16 + 2*16 + 3*1 = 256 + 32 + 3 = 291 (in base 10)
</pre>
<p>In base 16, "a" is the 10th symbol and "f" is the 15th.  Make sure that you can verify that the results I have shown you above are correct!</p>
<p>Before we leave this lesson, and especially this section, I should say a few words about binary numbers, or numbers expressed in base 2.  Binary numbers are made up of the following two symbols: "0 and 1".  Here's a binary number and its translation in base 10:</p>
<pre>
101001 = 1*2**5 + 0*2**4 + 1*2**3 + 0*2**2 + 0*2**1 + 1*2**0
or
101001 = 1*32 + 1*8 + 1*1 = 41 (in base 10)
</pre>
<p>Binary numbers are very important ... no, they are <b>essential</b> for computers; the reason is fairly simple to understand, if we stick to a very basic description.</p>
<p>Think of a lightbulb and a light switch.  If the light switch is on, the lightbulb gives light; that is because an electric current goes through it.  If the light switch if off, the lightbulb does not give any light; no electric current goes through it.  There are only two possibilities.  The usual convention is the following: no current = 0; current goes through = 1.</p>
<p>Now, inside computers, we do not have lightbulbs, but there are miniature wires and switches <small>[and other electronic components which are not important for our discussion]</small>. The state of a computer at a certain instant can be given in terms of which wires (say the 3rd, the 5th, the 17th, ...) that have a current going through them.  This could be represented as a series of 0s (wires with no current) and 1s (wires with current).  This state corresponds to a certain number ... expressed in base 2!  Computer programs essentially control the tiny switches that make currents go through the computer change with time.</p>
<p>Now, because 8 is a power of 2 (8=2*2*2), octal numbers are another useful representation of the natural numbers for computers.  Similary for hexadecimal numbers (16=2*2*2*2).  Because they are shorter (for us) to write, hexadecimal numbers are most often used, after decimal numbers, by programmers.  You will likely get to use them when you start writing your own games using Python!</p>
<center>
<a href="../lessons_toc.htm"><img alt="home"  src="../../images/home.png"></a>
</center>
</body>
</html>
